import Vue from 'vue'
import Vuex from 'vuex'
import router from '../router/index'
Vue.use(Vuex)
const tabs = JSON.parse(localStorage.getItem('tab'))
const tab = [
  {
    id: 1,
    name: "首页",
    path: "/admin",
  },
]
export default new Vuex.Store({
  state: {
    loading: false,
    tabIndex: localStorage.getItem("tabIndex")??0,
    tabList: tabs ? tabs.length > 0 ? tabs : tab : tab
  },
  mutations: {
    openLoading(state) {
      state.loading = true;
    },
    closeLoading(state) {
      state.loading = false;
    },
    setTabIndex(state, val) {
      state.tabIndex = val
      localStorage.setItem("tabIndex",state.tabIndex)
      router.push(state.tabList[state.tabIndex].path)
    },
    addTab(state, tab) {
      const index = state.tabList.findIndex(e => {
        return tab.path == e.path
      })
      console.log(index);
      if (index != -1) {
        state.tabIndex = index
      } else {
        state.tabList.push(tab)
        state.tabIndex = state.tabList.length - 1
      }
      console.log(state.tabIndex);
      localStorage.setItem('tab', JSON.stringify(state.tabList))
    },
    delTab(state, index) {
      if (index <= state.tabIndex) {
        state.tabIndex = state.tabIndex - 1;
        router.push(state.tabList[state.tabIndex].path)
      }
      state.tabList.splice(index, 1);
      console.log(state.tabIndex);
      localStorage.setItem('tab', JSON.stringify(state.tabList))
    }

  },
  actions: {
  },
  modules: {
  }
})
